//
//  JRHttpRequest.m
//  
//
//  Created by dingjiarui on 2021/7/26.
//

#import "JRHttpRequest.h"

JRAFHTTPRequestMethod const JRAFHTTPRequestMethodGet = @"GET";
JRAFHTTPRequestMethod const JRAFHTTPRequestMethodPost = @"POST";
JRAFHTTPRequestMethod const JRAFHTTPRequestMethodPut = @"PUT";
JRAFHTTPRequestMethod const JRAFHTTPRequestMethodDelete = @"DELETE";

JRAFHTTPRequestMethod const JRAFHTTPRequestMethodJsonGet = @"JSON_GET";
JRAFHTTPRequestMethod const JRAFHTTPRequestMethodJsonPost = @"JSON_POST";

const JRHttpRequest JRHttpRequestGet = {
    JRAFHTTPRequestMethodGet,
    0,
    nil,
    nil,
    NSOperationQueuePriorityNormal,
    nil

};

NSString * const JRHttpCallBack_FirstObject = @"JRHttpCallBack_FirstObject_0";
NSString * const JRHttpCallBack_SecondObject = @"JRHttpCallBack_FirstObject_1";

@interface JRHttpCallBack ()

@property (nonatomic, strong) NSDictionary *successDict;
@property (nonatomic, strong) NSDictionary *failureDict;
@property (nonatomic, strong) NSDictionary *trafficDict;
@property (nonatomic, strong) NSDictionary *downloadDict;
@property (nonatomic, strong) NSDictionary *uploadDict;

@property (nonatomic, copy, nullable) JRHttpSuccessBlock success;
@property (nonatomic, copy, nullable) JRHttpFailureBlock failure;
@property (nonatomic, copy, nullable) JRHttpTrafficBlock traffic;
@property (nonatomic, copy, nullable) JRHttpDownloadBlock download;
@property (nonatomic, copy, nullable) JRHttpUploadBlock upload;

@end

@implementation JRHttpCallBack

- (instancetype)init {
    
    self = [super init];
    
    if (self) {
        
        __weak typeof(self) weakSelf = self;
        
        _success = ^(NSURLResponse * _Nonnull response, id _Nullable responseObject) {
            if (responseObject != nil) {
                weakSelf.successDict = @{
                    JRHttpCallBack_FirstObject:response,
                    JRHttpCallBack_SecondObject: responseObject
                };
            } else {
                weakSelf.successDict = @{
                    JRHttpCallBack_FirstObject:response
                };
            }
        };
        
        _failure = ^(NSError * _Nullable error) {
            if (error != nil) {
                weakSelf.failureDict = @{
                    JRHttpCallBack_FirstObject: error
                };
            } else {
                weakSelf.failureDict = @{};
            }
        };
        
        _traffic = ^(long long sendTraffic, long long receiveTraffic) {
            weakSelf.trafficDict = @{
                JRHttpCallBack_FirstObject: @(sendTraffic),
                JRHttpCallBack_SecondObject: @(receiveTraffic)
            };
        };
        
        _download = ^(long long totalBytes, long long totalBytesExpected) {
            weakSelf.downloadDict = @{
                JRHttpCallBack_FirstObject: @(totalBytes),
                JRHttpCallBack_SecondObject: @(totalBytesExpected)
            };
        };
        
        _upload = ^(long long totalBytes, long long totalBytesExpected) {
            weakSelf.uploadDict = @{
                JRHttpCallBack_FirstObject:@(totalBytes),
                JRHttpCallBack_SecondObject:@(totalBytesExpected)
            };
        };
        
    }
    
    return self;
}

#pragma mark - JRHttpCallBackConvertible

- (id<JRHttpCallBackConvertible>  _Nonnull (^)(JRHttpSuccessBlock _Nonnull))onSuccess {
    
    return ^id(JRHttpSuccessBlock success) {
        
        if (success && self.successDict) {
            success(self.successDict[JRHttpCallBack_FirstObject], self.successDict[JRHttpCallBack_SecondObject]);
        }
        
        self.successDict = nil;
        self.success = success;
        
        return self;
    };
    
}

- (id<JRHttpCallBackConvertible>  _Nonnull (^)(JRHttpFailureBlock _Nonnull))onFailure {
    
    return ^id(JRHttpFailureBlock failure) {
        
        if (failure && self.failureDict) {
            failure(self.failureDict[JRHttpCallBack_FirstObject]);
        }
        
        self.failureDict = nil;
        self.failure = failure;
        
        return self;
    };
    
}

- (id<JRHttpCallBackConvertible>  _Nonnull (^)(JRHttpTrafficBlock _Nonnull))onTraffic {
    
    return ^id(JRHttpTrafficBlock traffic) {
        
        if (traffic && self.trafficDict) {
            traffic(self.trafficDict[JRHttpCallBack_FirstObject], self.trafficDict[JRHttpCallBack_SecondObject]);
        }
        
        self.trafficDict = nil;
        self.traffic = traffic;
        
        return self;
    };
    
}

- (id<JRHttpCallBackConvertible>  _Nonnull (^)(JRHttpProgressBlock _Nonnull))onDownload {
    
    return ^id(JRHttpProgressBlock download) {

        if (download && self.downloadDict) {
            download(self.downloadDict[JRHttpCallBack_FirstObject], self.downloadDict[JRHttpCallBack_SecondObject]);
        }
        
        self.downloadDict = nil;
        self.download = download;
        
        return self;
        
    };
}

- (id<JRHttpCallBackConvertible>  _Nonnull (^)(JRHttpProgressBlock _Nonnull))onUpload {
    
    return ^id(JRHttpProgressBlock upload) {
        
        if (upload && self.uploadDict) {
            upload(self.uploadDict[JRHttpCallBack_FirstObject], self.uploadDict[JRHttpCallBack_SecondObject]);
        }
        
        self.uploadDict = nil;
        self.upload = upload;
        
        return self;
    };
    
}
@end
